package com.qiaotong.colorpicker.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class WheelColorPicker extends View {
    private Paint colorRingPaint;
    private Paint centerCirclePaint;
    private Paint selectorPaint;

    private float ringWidth;
    private int[] colors;
    private float selectorPosition;
    private OnColorChangedListener listener;

    public interface OnColorChangedListener {
        void onColorChanged(int color);
    }

    public WheelColorPicker(Context context) {
        super(context);
        init();
    }

    public WheelColorPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 初始化颜色环的颜色（HSV色相环）
        colors = new int[360];
        for (int i = 0; i < 360; i++) {
            colors[i] = Color.HSVToColor(new float[]{i, 1f, 1f});
        }

        // 颜色环画笔
        colorRingPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        colorRingPaint.setStyle(Paint.Style.STROKE);
        colorRingPaint.setStrokeCap(Paint.Cap.ROUND);

        // 中心圆画笔
        centerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        centerCirclePaint.setStyle(Paint.Style.FILL);
        centerCirclePaint.setColor(Color.RED); // 默认颜色

        // 选择器画笔
        selectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        selectorPaint.setStyle(Paint.Style.FILL);
        selectorPaint.setColor(Color.WHITE);
        selectorPaint.setStrokeWidth(2);
        selectorPaint.setStyle(Paint.Style.STROKE);

        ringWidth = dpToPx(20); // 环的宽度
        selectorPosition = 0; // 默认在0度位置
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        ringWidth = (float) centerX /2;

        int radius = Math.min(centerX, centerY) - (int) (ringWidth / 2);

        // 绘制颜色环
        drawColorRing(canvas, centerX, centerY, radius);

        // 绘制中心圆
//        drawCenterCircle(canvas, centerX, centerY, (int)(radius - ringWidth));
        drawCenterCircle(canvas, centerX, centerY, radius/3);

        // 绘制选择器
        drawSelector(canvas, centerX, centerY, radius);
    }

    private void drawColorRing(Canvas canvas, int centerX, int centerY, int radius) {
        RectF rectF = new RectF(centerX - radius, centerY - radius,
                centerX + radius, centerY + radius);

        // 使用SweepGradient创建渐变色环
        SweepGradient sweepGradient = new SweepGradient(centerX, centerY, colors, null);
        colorRingPaint.setShader(sweepGradient);
        colorRingPaint.setStrokeWidth(ringWidth);

        canvas.drawArc(rectF, 0, 360, false, colorRingPaint);
    }

    private void drawCenterCircle(Canvas canvas, int centerX, int centerY, int radius) {
        canvas.drawCircle(centerX, centerY, radius, centerCirclePaint);
    }

    private void drawSelector(Canvas canvas, int centerX, int centerY, int radius) {
        float angle = (float) Math.toRadians(selectorPosition);
        float selectorX = centerX + (radius) * (float) Math.cos(angle);
        float selectorY = centerY + (radius) * (float) Math.sin(angle);

        canvas.drawCircle(selectorX, selectorY, ringWidth / 6, selectorPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                updateColor(event.getX(), event.getY());
                return true;
        }
        return super.onTouchEvent(event);
    }

    private void updateColor(float x, float y) {
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        // 计算触摸点相对于中心的角度
        double angle = Math.atan2(y - centerY, x - centerX);
        float degree = (float) Math.toDegrees(angle);
        if (degree < 0) {
            degree += 360;
        }

        selectorPosition = degree;

        // 更新中心圆颜色
        int selectedColor = getColorAtAngle(degree);
        centerCirclePaint.setColor(selectedColor);

        // 回调监听器
        if (listener != null) {
            listener.onColorChanged(selectedColor);
        }

        invalidate();
    }

    private int getColorAtAngle(float degree) {
        int index = (int) (degree * colors.length / 360) % colors.length;
        return colors[index];
    }

    public void setOnColorChangedListener(OnColorChangedListener listener) {
        this.listener = listener;
    }

    public int getSelectedColor() {
        return centerCirclePaint.getColor();
    }

    public void setSelectedColor(int color) {
        centerCirclePaint.setColor(color);
        invalidate();
    }

    private float dpToPx(float dp) {
        return dp * getResources().getDisplayMetrics().density;
    }
}